package cn.wostore.wxhelper.adapter;

import android.graphics.Typeface;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import cn.wostore.wxhelper.R;
import cn.wostore.wxhelper.bean.Group;
import cn.wostore.wxhelper.bean.SelectGroup;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuch
 * @date 2019/1/2 0002
 */
public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.VH> implements Filterable {
    private List<SelectGroup> groups;
    private List<SelectGroup> filterGroups;
    private SelectGroup selectedGroup;
    private OnItemClickListener onItemClickListener;

    public GroupAdapter(List<Group> groups) {
        initGroups(groups);
    }

    private void initGroups(List<Group> groups) {
        this.groups = new ArrayList<>();
        this.filterGroups = new ArrayList<>();
        for (int i = 0; i < groups.size(); i++) {
            this.groups.add(new SelectGroup(groups.get(i), false, i));
            this.filterGroups.add(new SelectGroup(groups.get(i), false, i));
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setSelection(int position) {
        if (filterGroups.get(position).isSelected()) {
            for (SelectGroup selectGroup : groups) {
                selectGroup.setSelected(false);
            }
            filterGroups.get(position).setSelected(false);
            selectedGroup = null;
        } else {
            for (SelectGroup selectGroup : filterGroups) {
                selectGroup.setSelected(false);
            }
            filterGroups.get(position).setSelected(true);
            for (SelectGroup selectGroup : groups) {
                selectGroup.setSelected(false);
                if(selectGroup.getIndex()==filterGroups.get(position).getIndex())
                    selectGroup.setSelected(true);
            }
            selectedGroup = filterGroups.get(position);
        }
        notifyDataSetChanged();
    }

    public int getSelect() {
        if (selectedGroup != null)
            return selectedGroup.getIndex();
        else
            return -1;
    }

    public Group getSelectGroup() {
        if (selectedGroup != null)
            return selectedGroup.getGroup();
        else
            return null;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_item, null);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH vh, int i) {
        Picasso.get().load(Uri.parse(filterGroups.get(i).getGroup().getImgUrl())).tag(vh).into(vh.imgPhoto);
        vh.nameTextView.setText(filterGroups.get(i).getGroup().getChatroomName());
        vh.stateCheckBox.setChecked(filterGroups.get(i).isSelected());
        vh.itemView.setSelected(filterGroups.get(i).isSelected());
        if (filterGroups.get(i).isSelected())
            vh.nameTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        else
            vh.nameTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        if (onItemClickListener != null) {
            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(vh.itemView, vh.getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return filterGroups.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString = constraint.toString();
                if (charString.isEmpty()) {
                    filterGroups = groups;
                } else {
                    List<SelectGroup> filteredList = new ArrayList<>();
                    for (int i = 0; i < groups.size(); i++) {
                        if (groups.get(i).getGroup().getChatroomName().contains(charString)) {
                            filteredList.add(groups.get(i));
                        }
                    }
                    filterGroups = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filterGroups;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filterGroups = (List<SelectGroup>) results.values;
                notifyDataSetChanged();
            }
        };
    }

    static class VH extends RecyclerView.ViewHolder {
        ImageView imgPhoto;
        TextView nameTextView;
        CheckBox stateCheckBox;


        VH(View itemView) {
            super(itemView);
            imgPhoto = itemView.findViewById(R.id.iv_photo);
            nameTextView = itemView.findViewById(R.id.tv_name);
            stateCheckBox = itemView.findViewById(R.id.cb_state);
        }
    }
}
